import os
import precedent_values as pv
import find_specific_file as fej
import load_entry_content as lec
import handle_new_title as hnt
import write_ffmpeg_cmd as wfc
import write_mv_cmd as wmc
import get_platform
import write_dict_file as wdf
import error_messages as e_m


def check_directory_exists(directory_path):
    if os.path.exists(directory_path):
        print("\ncheck_directory_exists.directory_path")
        return directory_path
    else:
        print("\ncheck_directory_exists.not_found_directory")
        return e_m.not_found_directory


def write_into_json_file(material_folder, summary_dictionary):
    output_json = material_folder.replace("\\", "-").replace("/", "-").replace(":", "-")
    output_json += ".json"
    print("\n", "output_json_file:", output_json)

    wdf.write_dict_to_json_file(
        summary_dictionary, pv.recording_file_folder, output_json
    )


def pivot(material_folder, pattern_selected, output_video_path):
    output_video_path = check_directory_exists(output_video_path)

    entry_file = fej.find_entry_json(material_folder, pv.entry_json_file)
    print("\nentry_file:", entry_file)

    cont_dict = lec.json_to_dict(entry_file)
    video_avid = cont_dict["avid"]
    video_title = cont_dict["title"]
    video_page = cont_dict["page_data"]["page"]
    print(
        "\navid:", video_avid, "\ntitle:", video_title, "\npage_data.page:", video_page
    )

    pattern_name = "Sound" if pattern_selected == pv.sound_pattern else "Silent"
    video_new_name = hnt.generate_new_name(
        video_avid,
        video_title,
        video_page,
        pattern_name,
        "mp4",
        replace_elements=pv.replace_elements,
    )
    print("\n", "video_new_name:", video_new_name)

    # 拟定新文件名绝对路径
    new_absolute_name = output_video_path + os.sep + video_new_name
    print("\n", "new_absolute_name:", new_absolute_name)

    source_video_file = fej.find_entry_json(material_folder, pv.video_file)
    print("\n", "source_video_file:", source_video_file)

    source_audio_file = fej.find_entry_json(material_folder, pv.audio_file)
    print("\n", "source_audio_file:", source_audio_file)

    executed_cmd = ""
    if pattern_selected == pv.sound_pattern:
        executed_cmd = wfc.build_command_line(
            new_absolute_name, source_video_file, source_audio_file
        )
    elif pattern_selected == pv.silent_pattern:
        move_cmd = get_platform.get_move_command()
        executed_cmd = wmc.create_string(move_cmd, source_video_file, new_absolute_name)
    print("\n", "executed_cmd:", executed_cmd)

    summary_dictionary = dict(
        entry_file=entry_file,
        video_avid=video_avid,
        video_title=video_title,
        video_page=video_page,
        video_new_name=video_new_name,
        source_video_file=source_video_file,
        source_audio_file=source_audio_file,
        executed_cmd=executed_cmd,
        material_folder=material_folder,
        pattern_selected=pattern_selected,
        new_absolute_name=new_absolute_name,
        output_video_path=output_video_path,
    )

    print("\n", "summary_dictionary:", summary_dictionary)
    write_into_json_file(material_folder, summary_dictionary)
    return summary_dictionary
